<template>
  <div class="common-layout">
    <el-container>
      <el-header>Header</el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-active="activeMenu" @select="handleMenuSelect">
            <el-menu-item
              v-for="route in menuRoutes"
              :key="route.path"
              :index="route.path"
            >
              <i :class="route.meta.icon"></i>
              <span>{{ route.meta.title }}</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { useRouter, useRoute } from "vue-router";

export default {
  name: "LayoutContainer",
  setup() {
    const router = useRouter();
    const route = useRoute();

    // 获取动态路由
    const menuRoutes = router.getRoutes().filter((route) => route.meta.title);

    // 当前激活的菜单
    const activeMenu = route.path;

    // 菜单选择事件
    const handleMenuSelect = (path) => {
      router.push(path);
    };

    return {
      menuRoutes,
      activeMenu,
      handleMenuSelect,
    };
  },
};
</script>

<style scoped>
.common-layout {
  height: 100vh;
}
.el-header {
  background-color: #409eff;
  color: #fff;
  line-height: 60px;
}
.el-aside {
  background-color: #e9eef3;
}
.el-main {
  background-color: #fff;
}
</style>
